#!/bin/bash
#
# Slackware build script for listener.
#
# Distributed under the beerware licence:
#
# "THE BEER-WARE LICENSE" (Revision 42):
# pwcazenave <at> gmail <dot> com wrote this file. As long as you retain this
# notice you can do whatever you want with this stuff. If we meet some day,
# and you think this stuff is worth it, you can buy me a beer in return
# Pierre Cazenave
#
# Created 07/08/2008
# Updated for Slackware64 28/05/2009
# Updated to version 2.0.1 09/10/2011
#

# 20211224 bkw: BUILD=2, fix for latest portaudio.
# 20170813 bkw:
# - take over maintenance. If you want to buy me a beer too, I won't stop you!
# - update for v2.2 (which has been out for 4+ years now)
# - include filter plugins in package
# - add man page
# - script cleanups

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=listener
VERSION=${VERSION:-2.2}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar -xvf $CWD/$PRGNAM-$VERSION.tgz
cd $PRGNAM-$VERSION
chown -R root:root .
find . -type f -exec chmod 644 {} +
find . -type d -exec chmod 755 {} +

# 20211224 bkw: PortAudio API has changed: we have to call
# Pa_GetDefaultInputDevice() to get the default input device,
# it's no longer device #0. This matters because e.g. on my system,
# device zero is the HDMI output to my monitor's speakers... which of
# course doesn't have a microphone or line input. Ideally, listener
# would let you list the input devices and choose the one you want,
# but that's more work than I want to do right now. You could set
# PA_RECOMMENDED_OUTPUT_DEVICE in the environment, but there's no
# handy tool to list the available device numbers.
patch -p1 < $CWD/portaudio_default_input_device.diff

# fix hardcoded config file path
sed -i 's,/usr/local/etc,/etc,g' $PRGNAM.h manual.html

# search for filter plugins here:
FILTERDIR=/usr/lib$LIBDIRSUFFIX/$PRGNAM
sed -i "s|-ldl|-Wl,-rpath,$FILTERDIR &|" Makefile

# build the main program
CFLAGS="$SLKCFLAGS" make

# build (some of) the example filters
mkdir -p $PKG/$FILTERDIR
for i in deglitch highpass lowpass unspike; do
  cd filter_lib_examples/$i
    sed -i "s,^CFLAGS=,&$SLKCFLAGS ," Makefile
    CFLAGS="$SLKCFLAGS" make
    install -s *.so.* $PKG/$FILTERDIR
  cd -
done

# easier to install manually than coerce 'make install' to play nice.
mkdir -p $PKG/usr/bin $PKG/etc
install -s -m0755 $PRGNAM set$PRGNAM $PKG/usr/bin
cat $PRGNAM.conf > $PKG/etc/$PRGNAM.conf.new

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a license.txt manual.html $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
